package com.lesama.lock;

/*
    同步代码块
    synchronized(锁对象) {
        多条语句操作共享数据的代码
    }
 */

public class ThreadTest1 {
    public static void main(String[] args) {
        SellTicketsTask1 task1 = new SellTicketsTask1();

        Thread t1 = new Thread(task1, "线程1");
        Thread t2 = new Thread(task1, "线程2");
        Thread t3 = new Thread(task1, "线程3");

        t1.start();
        t2.start();
        t3.start();
    }
}

class SellTicketsTask1 implements Runnable {

    private int tickets = 10000;
    private Object o = new Object();

    @Override
    public void run() {
        while(true) {
            synchronized (o) {
                if(tickets == 0) {
                    break;
                }
                System.out.println(Thread.currentThread().getName() + "售出了第" + tickets + "张票");
                tickets--;
            }
        }
    }
}
